home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / DOSTIPS6.ZIP / DOSBATCH < prev    next >
Text File  |  1987-03-03  |  19KB  |  488 lines

  1.                              Get SET
  2.        (PC Magazine Vol 6 No 1 Jan 13, 1987 User-to-User)
  3.  
  4.      You can access the SET environment strings in batch files simply
  5. by putting the set name inside a pair of percent signs (%).  This lets
  6. batch files use the environment as a global memory area.  For example,
  7. with a subdirectory on drive A: called |DOS, type:
  8.  
  9. SET DOIT=A:\DOS
  10.  
  11. Then create a batch file called D.BAT:
  12.  
  13. DIR %DOIT%
  14.  
  15. Typing D at the DOS prompt will display the directory listing for
  16. A:\DOS.
  17.      Editor's Note:  SET has been around for a long time as an
  18. undocumented command, and from the pathetic description in the DOS
  19. 3.x manual, it might just as well be undocumented.  This technique
  20. does work but just scratches the surface.  You can see the current
  21. state of your environment, including all the variables you've set,
  22. by typing SET.
  23.  
  24. -----------------------------------------------------------------
  25.                        Easy Global Erasing
  26.        (PC Magazine Vol 6 No 2 Jan 27, 1987 User-to-User)
  27.  
  28.      If you frequently delete all the files in the current directory
  29. with the command DEL *.* and are tired of answering the question, "Are
  30. you sure (Y/N)?", try the file DALL.BAT:
  31.  
  32. ECHO OFF
  33. REN *.* $*.* >NUL
  34. DEL $*.* >NUL
  35. IF EXIST *.* DEL *.* >NUL
  36.  
  37.      This fools DOS into thinking it's deleting just a subset of all
  38. the files in the directory.  It renames all the files to have a "$" as
  39. the first character, then deletes those files.  The >NUL sends the "n
  40. files deleted" message into the Twilight Zone.
  41.      In the rare cases that two filenames differ only in the first
  42. letter, or if the first letter is "$", some files will not get deleted.
  43. Therefore, after the first delete, we delete anything left over.  In
  44. real life this almost never occurs, but if it does all that will happen
  45. is that you'll get the "Are you sure (Y/N)?" prompt.
  46.      To enhance this so that you get no error messages if you run it
  47. on an empty directory, IF EXIST clauses may be added to the REN and
  48. DEL commands.
  49.      Editor's Note:  You could erase all the files in a directory with
  50. this ERASE.BAT file:
  51.  
  52. ECHO OFF
  53. FOR %%F IN (*.*) DO DEL %%F >NUL
  54.  
  55.      If you omit a carriage return from the end of the second line,
  56. you'll see each file deleted one by one.  However, if you include a
  57. carriage return, while you won't see the individual file deletion
  58. messages, you'll get a "Batch file missing" error message.
  59.      The technique described above is clever, but it won't work
  60. properly since it will delete itself halfway through the process and
  61. grind to a halt.  It's possible to adapt it, with a pair of batch
  62. files.  First, DALL.BAT:
  63.  
  64. ECHO OFF
  65. MD UPONE
  66. CD UPONE
  67. COPY ..\DELALL.BAT >NUL
  68. COMMAND /C DELALL
  69. CD ..
  70. DEL UPONE\DELALL.BAT
  71. RD UPONE
  72. DEL DALL.BAT
  73.  
  74. Next, DELALL.BAT:
  75.  
  76. COPY ..\DALL.BAT >NUL
  77. :START
  78. CTTY NUL
  79. REN ..\*.* $*.*
  80. CTTY CON
  81. DEL ..\$*.*
  82. IF EXIST ..\*.* GOTO START
  83. COPY DALL.BAT .. >NUL
  84. DEL DALL.BAT
  85.  
  86.      The idea intended here is to create another subdirectory that is
  87. located one level up and copy the batch file in and out, but that's
  88. extremely clunky.  The one real improvement is that if the DELALL.BAT
  89. file does trip over two files that are exactly the same except for the
  90. first letter, it will simply start over again.  DELALL.BAT assumes that
  91. you don't have any files that begin with $.  If you did have such
  92. files, you could also add a third IF statement to rename them to
  93. something else and then delete those -- but the two batch files are
  94. overly complex already.
  95.      Also, be sure to note that >NUL won't suppress error messages,
  96. but CTTY NUL will.  If you use CTTY NUL be very careful to bring your
  97. system back with the command CTTY CON.  And even this technique won't
  98. handle the missing batch file error you'll get if you insert a carriage
  99. return at the end of the last line of DELALL.BAT.
  100.      The obvious solution to this problem is to create a one-line batch
  101. file that you use from one subdirectory to erase all the files in
  102. another subdirectory.  If you're in \1\2\3 and you want to delete
  103. everything in \1\2\3\4, you can do it with the batch file containing
  104. the line:
  105.  
  106. DEL \1\2\3\4*.* < Y
  107.  
  108.      For this solution to work you would need a tiny file on \1\2\3
  109. called Y that contained simply two characters -- a Y and a carriage
  110. return.  If you wanted, you could try this with replaceable parameters,
  111. but you'd have to be careful that you didn't erase the wrong files
  112. through a silly but fatal typing error.
  113.  
  114. -----------------------------------------------------------------
  115.                       Real Batch Variables
  116.        (PC Magazine Vol 6 No 3 Feb 10, 1987 User-to-User)
  117.  
  118.      DOS can use replaceable parameters in batch files but has no way
  119. to turn replaceable parameters into true variables.  It can be done --
  120. this one works with DOS 3.1 only!
  121.      Use the DEBUG mini-assembler and redirect the INPUT.SCR file:
  122.  
  123. DEBUG < INPUT.SCR
  124.  
  125. THis will create a modified version of COMMAND.COM called ALTCOM.COM.
  126. ALTCOM.COM uses the space originally taken up by the VER and RMDIR
  127. commands and replaces these with a new INPUT command.  (You'll still
  128. be able to use RD to remove subdirectories, but not RMDIR.  And VER
  129. will be missing a space or two, as well as its trailing carriage
  130. return.)
  131.  
  132. N COMMAND.COM
  133. L
  134. A 246E
  135. MOV    SI,3C03
  136. INC    CX
  137. SUB    [SI],CL
  138. MOV    DX,SI
  139. MOV    AX,0C0A
  140. INT    21
  141. INC    SI
  142. DEC    DI
  143. LODSB
  144. MOV    CL,AL
  145. INC    CX
  146. REPZ    MOVSB
  147. CALL    29DG
  148. JMP    270F
  149.  
  150. E 4D9B "INPUT",02,2E,11
  151. E 4CEB 49
  152. E 4D6D 49
  153. E 4A4E "3.1",00,00,00,00,00,00
  154. N ALTCOM.COM
  155. W
  156. Q
  157.  
  158.      The syntax for INPUT is:
  159.  
  160. INPUT variable
  161.  
  162. When DOS sees the INPUT command in a batch file, it will wait for input
  163. from the keyboard.  This information will then be placed in the DOS
  164. environment for the batch file to use.
  165.      To see this in action, create the following TEST.BAT batch file.
  166. (Remember, this works in DOS 3.1 only.)
  167.  
  168. ECHO OFF
  169. CLS
  170. :LOOP
  171. ECHO COMMAND?
  172. INPUT CMD=
  173. IF %CMD%! == ! GOTO LOOP
  174. IF %CMD% == STOP GOTO END
  175. IF %CMD% == stop GOTO END
  176. %CMD%
  177. GOTO LOOP
  178. :END
  179.  
  180.      Before you try it, however, install your new command processor by
  181. typing in ALTCOM at the DOS prompt.  Then run the batch file.  To get
  182. out of the batch file just type STOP (or, stop).
  183.      The number of variables you can use is limited only by the amount
  184. of environment space you have.  If you like this, you can rename
  185. ALTCOM.COM to COMMAND.COM and use it to replace your old command
  186. processor.
  187.      If you type:
  188.  
  189. SET <parameter1>=<parameter2>
  190.  
  191. and the name of parameter1 is longer than the environment space, DOS
  192. responds with an "Out of environment space" error.  This is fine but
  193. when you type SET to find out what you have in the environment, you
  194. find the environment contains part of the name of parameter1.  Also
  195. you can no longer set anything else into the environment unless you
  196. erase something previously put in.  Besides, there is no way of
  197. removing that string short of restarting the computer.
  198.      Editor's Note:  This patch is interesting, even though it mangles
  199. some existing commands.  DOS batch files are powerful tools, but users
  200. sorely miss this kind of interaction.  The sample TEST.BAT file above
  201. lets you execute interactive DOS commands on the fly or pass parameters
  202. all around your system.
  203.      If you try this, remember you have to load ALTCOM.COM as a
  204. secondary command processor to make it work (type EXIT to return to
  205. yoru original COMMAND.COM when you're done).
  206.      Finally, in DOS 3.2, you can set environment size, using a
  207. /E COMMAND.COM switch.  And if you're running short of space in your
  208. environment, you can always use the SUBST command to shorten your
  209. paths, which are the big space hogs.
  210.  
  211. -----------------------------------------------------------------
  212.                     Well-Behaved Batch Files
  213.     (COMPUTE! Magazine February 1987 by Ulf Larsson-Westlund)
  214.  
  215.      YORN.COM helps make batch files more interactive.  When invoked
  216. from DOS, it displays a yes/no prompt and waits for you to press an
  217. indicated key, returning an error code which the batch file can use
  218. to branch to different parts of the command process.
  219.      To see what YORN.COM does, type YORN at the DOS prompt and press
  220. Enter.  The computer displays the prompt: Answer (Y)es or (N)o ...
  221. and waits for you to indicate your choice.  YORN.COM recognizes only
  222. the characters Y, y, N, or n.
  223.      An example will show how YORN.COM works in this YESNO.BAT file:
  224.  
  225. ECHO OFF
  226. CLS
  227. :START
  228. ECHO THIS IS A TEST BATCH FILE FOR YORN.COM
  229. YORN PLEAS PRESS N TO CONTINUE ...
  230. IF ERRORLEVEL 255 GOTO WRONG
  231. ECHO
  232. ECHO YOU PRESS THE N KEY
  233. GOTO END
  234. :WRONG
  235. ECHO
  236. ECHO YOU DIDN'T PRESS THE N KEY
  237. GOTO START
  238. :END
  239. ECHO
  240. ECHO ...ENDING
  241.  
  242.      Type YESNO at the DOS prompt.  Note that YORN.COM displays a
  243. different prompt this time.  Instead of Answer (Y)es or (N)o, it
  244. prints the message PLEASE PRESS N TO CONTINUE.  If you answer "Yes"
  245. by pressing Y or y, one series of batch commands is executed.  If you
  246. answer "No~ by pressing N or n, the batch file branches to a different
  247. series of commands.
  248.      It's not difficult to see how this capability might be useful.
  249. For instance, say you often boot up with an AUTOEXEC.BAT file that
  250. installs an accessory program such as SideKick.  When you use memory-
  251. intensive software such as Framework on a machine with only a limited
  252. amount or RAM, you may find yourself running out of memory if SideKick
  253. or a similar accessory is resident.  With YORN.COM, your AUTOEXEC.BAT
  254. file can ask you whether or not to install the accessory and respond
  255. accordingly.
  256.      YORN.COM tells you which key is pressed by returning an error
  257. code.  In YESNO.BAT, it returns an error code of 255 when you press Y
  258. or y and an error code of 254 for N or n.  You can check the error
  259. code with IF-ERRORLEVEL and branch to the desired destination with
  260. GOTO as shown in the sixth line of YESNO.BAT.  When you're checking
  261. error codes, it is essential to begin with the highest code (255 in
  262. this case) and work downward to lower codes systematically.
  263.      To change the prompt printed by YORN.COM, simply supply the text
  264. of the new prompt after the word YORN in the batch file.  If no such
  265. text is found, YORN.COM prints the default prompt.
  266.      For special purposes, you can also check for characters other
  267. than Y or N.  For instance, a batch process that can send output to
  268. either the screen or a disk file might prompt you to press S for
  269. screen output or D for disk output.
  270.      The hex numbers $59 and $79 in lines 390 and 400 of the BASIC
  271. program stand for the characters Y and y, respectively.  The hex
  272. numbers $4E and $6E in lines 400 and 410 stand for N and n,
  273. respectively.  To substitute other characters, replace these values
  274. with the values of the characters you wish to test for.  Remember that
  275. these numbers must be in hex.  (The BASIC function HEX$ converts
  276. decimal values to hex; for instance, PRINT HEX$(13) displays 0D, the
  277. hex equivalent of decimal 13.)  If you change any of these values, you
  278. must also change the checksum value (10731) in line 170 accordingly.
  279. Once this is done, rerun the BASIC program to create a new version of
  280. YORN.COM.
  281.  
  282. 100 'YORN.BAS:  Creates YORN.COM
  283. 120 PRINT "Checking DATA ....";
  284. 130 FOR I=0 TO 109
  285. 140 READ A$:A=VAL("&H"+A$)
  286. 150 CKSUM=CKSUM+A
  287. 160 NEXT I
  288. 170 IF CKSUM=10731 THEN 210
  289. 180 PRINT:PRINT
  290. 190 PRINT "Error; check your typing."
  291. 200 STOP
  292. 210 RESTORE 340
  293. 220 OPEN "YORN.COM" AS #1 LEN=1
  294. 230 FIELD #1,1 AS BYTE$
  295. 240 FOR I=0 TO 109
  296. 250 READ A$
  297. 260 LSET BYTE$=CHR$(VAL("&H"+A$))
  298. 270 PUT #1
  299. 280 NEXT I
  300. 290 CLOSE #1
  301. 300 PRINT:PRINT
  302. 310 PRINT "YORN.COM created."
  303. 320 PRINT
  304. 330 END
  305. 340 DATA EB,05,0D,20,20,1A,08,BE,80,00
  306. 350 DATA B5,00,8A,0C,83,F9,00,75,0A,BA
  307. 360 DATA 54,01,B4,09,CD,21,EB,19,90,46
  308. 370 DATA 8A,5C,01,80,FB,0D,74,08,8A,D3
  309. 380 DATA B4,02,CD,21,E2,EF,BA,6D,01,B4
  310. 390 DATA 09,CD,15,B4,00,CD,16,3C,59,74
  311. 400 DATA 11,3C,79,74,0D,3C,4E,74,04,3C
  312. 410 DATA 6E,75,EC,B0,FE,EB,03,90,B0,FF
  313. 420 DATA B4,4C,CD,21,41,6E,73,77,65,72
  314. 430 DATA 20,59,28,65,73,29,20,6F,72,20
  315. 440 DATA 4E,28,6F,29,20,2E,2E,2E,24,24
  316.  
  317. -----------------------------------------------------------------
  318.                        Batch File Bonanza
  319.              (PC World February 1987 Star-Dot-Star)
  320.  
  321.      DOS lacks an input command that can accept and pass a user's
  322. response to other DOS commands.  If this capability were available,
  323. a batch file could ask the operator if a particular command should
  324. exercise a given option during execution.
  325.      For example, the DIR command can be issued with the /P or /W
  326. option.  With an input command, a batch file could ask if the directory
  327. listing should pause when the screen fills or be displayed in wide
  328. format.  Once the batch file received a response, the DIR command
  329. would execute.
  330.      A DOS input command would also be handy in a batch-file-driven
  331. menu system.  A batch file could display a menu and rely on the input
  332. command to accept and execute the user's choice.
  333.      Until Microsoft includes such a command in its next version of
  334. DOS, you can achieve the same result -- capturing user input -- with
  335. the COPY command.  MENU.BAT is an example of how the technique could
  336. be applied to a menu system.  MENU.BAT begins by changing the DOS
  337. prompt to:
  338.  
  339. Enter A:MENU to display menu A>
  340.  
  341.      The program then tests for a parameter and checks whether it's
  342. valid or not.  When you call the batch file without a parameter (by
  343. simply typing:  MENU <Enter>), the first IF test branches execution
  344. to the label :getinput.  The following lines save the current DOS
  345. prompt in a DOS variable called TEMP; use PROMPT to command ANSI.SYS
  346. to redefine the <Enter> key as Ctrl-Z <Enter> and remove the DOS
  347. prompt; clear the screen; and use ECHO commands to display the menu.
  348. (Note that the ANSI.SYS command uses a lowercase p.)  (You must install
  349. the ANSI.SYS driver in your system's CONFIG.SYS file before the PROMPT
  350. command can redefine the keyboard.)
  351.     COPY CON:RESPONSE.DAT creates a file called RESPONSE.DAT containing
  352. the user's response entered from the keyboard.  (Ctrl-Z <Enter> must be
  353. used to inform the COPY command that the response is complete -- hence
  354. the redefined <Enter> key.)  >NUL suppresses the message '1 file(s)
  355. copied' by redirecting it to the nul (nonexistent) device.
  356.      The next COPY command concatenates COMMAND.DAT and RESPONSE.DAT,
  357. creating the file CONTINUE.BAT.  COMMAND.DAT simply contains the word
  358. MENU followed by a space.  Before you run MENU.BAT, create COMMAND.DAT
  359. in the default drive by typing:
  360.  
  361. COPY CON:COMMAND.DAT <Enter> MENU
  362.  
  363. and a single space.  Finish up by typing Ctrl-Z and <Enter>.
  364.      With CONTINUE.BAT in place, MENU.BAT uses PROMPT to restore the
  365. <Enter> key's original value, then removes the variable TEMP from the
  366. DOS environment, clears the screen, and calls the file CONTINUE.BAT,
  367. which it just created.
  368.      CONTINUE.BAT issues MENU, along with the user response stored in
  369. RESPONSE.DAT.  MENU.BAT runs anew, and because a parameter is supplied
  370. this time, the first IF test is negative and execution falls to the
  371. next IF test.  When a match is finally found, execution branches to
  372. the appropriate label and the desired menu item is executed.  If there
  373. is no match, MENU.BAT displays the message 'Invalid response'.  Press
  374. a key and the menu reappears, ready for input.
  375.  
  376. echo off
  377. prompt Enter A:MENU to display menu $_$n$g
  378. cls
  379. if %1!==!    goto getinput
  380. if %1!==1!   goto 1
  381. if %1!==2!   goto 2
  382. if %1!==3!   goto 3
  383. echo Invalid response
  384. pause
  385. :getinput
  386. set temp=%prompt%
  387. prompt $e[13;13;26;13p
  388. echo on
  389. echo off
  390. cls
  391. echo Enter your choice:
  392. echo -
  393. echo 1 for Microsoft Word
  394. echo 2 for Lotus 123
  395. echo 3 for dBASE III
  396. copy con:response.dat >nul
  397. copy command.dat+response.dat continue.bat >nul
  398. prompt $e[13;13p
  399. echo on
  400. echo off
  401. prompt %temp%
  402. set temp=
  403. cls
  404. continue
  405. :1
  406. word
  407. :2
  408. 123
  409. :3
  410. dbase
  411.  
  412. -----------------------------------------------------------------
  413.                         Batch Subroutines
  414.              (PC World February 1987 Star-Dot-Star)
  415.  
  416.      DOS does not support batch file subroutines -- in other words,
  417. when one batch file calls another, DOS cannot return control to the
  418. first batch file after the second one ends.
  419.      However, you can overcome this limitation by loading a second
  420. copy of COMMAND.COM, the DOS command processor, to execute the second
  421. batch file.  When the second batch file ends, the secondary command
  422. processor returns control to the first.  Execution resumes immediately
  423. after the line in the first batch file that invoked the second
  424. COMMAND.COM.
  425.      This technique is illustrated in CALL.BAT:
  426.  
  427. echo off
  428. echo This is the first batch file
  429. echo Ready to execute the first subroutine
  430. command/c sub1.bat
  431. echo Now back to the main batch file
  432. echo Transferring control to the second subroutine
  433. command/c sub2.bat
  434. echo Now back to the main batch file again
  435.  
  436. The first few lines display a message, then COMMAND/C SUB1.BAT executes
  437. the batch file SUB1.BAT:
  438.  
  439. echo This is the FIRST subroutine batch file
  440. pause
  441.  
  442. The /C parameter forces DOS to terminate COMMAND.COM when the named
  443. batch file ends.  (If you omit the /C switch, the last line of SUB1.BAT
  444. must be EXIT so that execution can be returned to the primary command
  445. processor; otherwise, commands continue to be processed by the
  446. secondary command processor.)  When SUB1.BAT ends, execution continues
  447. at the fifth line of CALLBAT.BAT, more messages are displayed and
  448. SUB2.BAT:
  449.  
  450. echo This is the SECOND subroutine batch file
  451. pause
  452.  
  453. is executed in the same manner.  When SUB2.BAT finishes, control
  454. returns to CALLBAT.BAT, which then ends.
  455.      This technique can be extended -- a second batch file calling a
  456. third, a third a fourth, and so on -- as long as memory is available
  457. and a copy of COMMAND.COM is loaded for each batch file.  Because every
  458. copy of COMMAND.COM creates a new environment, information such as ECHO
  459. status or DOS errorlevel value cannot be passed between batch files.
  460.      By using DOS redirection, a second copy of COMMAND.COM (and any
  461. program run by it) can receive input from a disk file instead of from
  462. the keyboard.  This feature makes it possible to automate virtually
  463. any operation using batch files.
  464.  
  465. -----------------------------------------------------------------
  466.                         Fast File Scanner
  467.        (PC Magazine Vol 6 No 6 Mar 31, 1987 User-to-User)
  468.  
  469.      A simple way to display lots of batch files is to create two batch
  470. files called SCANBATS.BAT and READ.BAT.  First, SCANBATS.BAT scans
  471. through all the .BAT files on a disk:
  472.  
  473. echo off
  474. for %%f in (*.bat) do command /c read %%f
  475.  
  476. Then, READ.BAT, which is called by SCANBATS.BAT, performs the actual
  477. displaying:
  478.  
  479. echo off
  480. cls
  481. echo %1
  482. type %1 | more
  483. pause
  484.  
  485. To try it, type in both files using a pure ASCII word processor or the
  486. DOS COPY CON command, and then type SCANBATS.
  487.  
  488.